/*************************************************************************
	> File Name: waitpid.c
	> Author: Angel Criss
	> Blog: blog.abbycin.tk 
	> Created Time: Mon 07 Jul 2014 05:06:07 PM CST
 ************************************************************************/

#include"head.h"

int main(int argc,char *argv[])
{
    pid_t child;
    int children,i,issue,status;

    children=(argc>1)?atoi(argv[1]):1;

    switch(child=fork())
    {
        case -1:
            err("fork");
        case 0:
            printf("(child) PID: %ld\n",(long)getpid());
            if(argc>1)
                _exit(0);
            else
                for(;;)
                    pause();
           exit(EXIT_FAILURE);
        default:
           for(;;)
           {
                issue=waitpid(child,&status,WUNTRACED
#ifdef WCONTINUED
                        |WCONTINUED
#endif
                        ); // not all system has this feature
                if(issue==-1)
                    err("waitpid");
                printf("(parent) waitpid return: (child) PID:%ld; status=%04x (%d,%d)\n",\
                    (long)child,\
                    (unsigned int)status,status>>8,status&0xff); // parent only care the low 8bit of child process's exit status
                 p_wait_status(NULL,status);
                if(WIFEXITED(status)||WIFSIGNALED(status))
                    exit(EXIT_SUCCESS);
           }
    }
}
